package com.alibaba.pet.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.uitl.*;
import com.alibaba.order.domain.OrderPetAcquisition;
import com.alibaba.order.mapper.OrderPetAcquisitionMapper;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.mapper.ShopMapper;
import com.alibaba.pet.domain.Pet;
import com.alibaba.pet.domain.SearchMasterMsg;
import com.alibaba.pet.domain.SearchMasterMsgAuditLog;
import com.alibaba.pet.dto.AcceptDto;
import com.alibaba.pet.dto.PublishDto;
import com.alibaba.pet.mapper.PetMapper;
import com.alibaba.pet.mapper.SearchMasterMsgAuditLogMapper;
import com.alibaba.pet.mapper.SearchMasterMsgMapper;
import com.alibaba.pet.service.ISearchMasterMsgService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.domain.User;
import com.alibaba.user.mapper.LogininfoMapper;
import com.alibaba.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-03-16
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper logMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public void publish(PublishDto dto) {

        // 校验参数
        // 将数据保存到数据库
        SearchMasterMsg msg = new SearchMasterMsg();
        BeanUtils.copyProperties(dto, msg);
        msg.setState(0);
        // 调用工具类 去拿到我们拦截里登录人的信息
        Logininfo logininfo = LoginContext.getLogininfo();
        User user = userMapper.findByPhone(logininfo.getPhone());
        msg.setUserId(user.getId());
        // 保存
        super.add(msg);
        // AI审核
        if (!BaiduAuditUtils.TextCensor(dto.getName())){
            // 审核不成功
            // 添加日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            msg.setState(2);
            log.setAuditId(-1L);
            log.setAuditTime(new Date());
            log.setMsgId(msg.getId());
            log.setState(0);
            log.setNote("寻主信息名称违规："+dto.getName());
            logMapper.add(log);
            return;
        }  else {
            msg.setState(4);
            // 50公里以内找店铺
            // 中心点 先找到用户的坐标
            Point point = DistanceUtil.getPoint(dto.getAddress());
            // 找到所有店铺
            List<Shop> shops = shopMapper.findAll();
            // 调用百度地图 找到50公里以内最近得店铺返回
            Shop nearestShop = DistanceUtil.getNearestShop(point,shops);
            // 找到
            if(null != nearestShop){
                // 将寻主消息分配给这个店铺 就是给msg的shopId赋值
                msg.setShopId(nearestShop.getId());
          }

        }
        super.update(msg);
        // 没找到

//        // 校验参数
//        // 将数据保存到数据库
//        SearchMasterMsg msg = new SearchMasterMsg();
//        BeanUtils.copyProperties(dto,msg);
//        msg.setState(0);
//        // 调用工具类 去拿到我们拦截里登录人的信息
//        Logininfo logininfo = LoginContext.getLogininfo();
//        User user = userMapper.findByPhone(logininfo.getPhone());
//        msg.setUserId(user.getId());
//        // 保存
//        super.add(msg);
//        // AI审核
//        if (!BaiduAuditUtils.TextCensor(msg.getName())){
//            //不通过
//            // 添加日志
//            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
//            log.setAuditId(-1L);
//            log.setState(1);
//            log.setMsgId(msg.getId());
//            log.setAuditTime(new Date());
//            log.setNote("名称不合格");
//            logMapper.add(log);
//            return;
//        }
//        // 50公里找店
//        // 那到我们申请的地址
//        Point point = DistanceUtil.getPoint(dto.getAddress());
//        // 在找50公里范围内所有的店
//        List<Shop> shops = shopMapper.findAll();
//        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
//        if (null != nearestShop){
//            // 将寻主消息分配给这个店铺 就是给msg的shopId赋值
//            msg.setShopId(nearestShop.getId());
//            super.update(msg);
//        }
    }


    /**
     *  id 是 msg id
     * @param id
     */
    @Override
    public void reject(Long id) {
        // 直接修改shopid 为null
       msgMapper.updateById(id);

    }

    /**
     *  接单
     * @param
     */
    @Override
    public void accept(AcceptDto dto) {
        // 参数校验
        // 查询msg
        SearchMasterMsg msg = msgMapper.finById(dto.getMsgId());
        //2.生成宠物信息
        Pet pet = new Pet();
        pet.setName(msg.getName());
        // TODO 这里面先设置成 收购价格 然后改价之后我们需要将这个价格同步修改
        pet.setCostprice(BigDecimal.valueOf(msg.getPrice()));
        pet.setState(0);
        pet.setTypeId((long)(msg.getPetType()));
        pet.setCreatetime(new Date());
        pet.setShopId(msg.getShopId());
        pet.setSearchMasterMsgId(dto.getMsgId());
        petMapper.add(pet);

        //1.生成收购单
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msg.getUserId()));
        order.setDigest("xxxx店铺接单啦");
        order.setLastConfirmTime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        order.setState(0);//待支付
        order.setPrice(BigDecimal.valueOf(msg.getPrice()));// 价格 收购价格 寻主信息里面的价格
        order.setPayType(1);
        order.setPetId(pet.getId());
        order.setUserId(msg.getUserId());// msg里面有
        order.setShopId(msg.getShopId()); // msg
        order.setAddress(msg.getAddress()); // msg
        order.setEmpId(dto.getHandler());
        order.setMsgId(dto.getMsgId());
        orderMapper.add(order);
        //3.msg的状态改成已接单
        msg.setState(3);
        msgMapper.update(msg);
    }
}
